package com.lp.controller;

import com.lp.po.User;
import com.lp.service.UserService;
import com.lp.userException.CustomException;
import com.lp.utils.Depository;
import com.lp.utils.MDUtils;
import com.lp.utils.RegistUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;

@Controller
@RequestMapping("/admin/Password")
public class AdminPasswordController {
    @Resource
    private UserService userService;
    RegistUtil registUtil=new RegistUtil();

//        if(!(username instanceof String)){
//        throw new GlobalException("异常演示");
//    }  str.indexOf(" ")!=-1

    /***************************账户密码设置****************************************/
    // 普通用户账号密码重置
    @RequestMapping("/userPasswordRest")
    public String userPasswordRestUI() throws Exception {
        return "/admin/userPasswordRest";
    }
    // 普通用户账号密码重置处理
    @RequestMapping(value = "/doUserPasswordRest", method = {RequestMethod.POST})
    public String userPasswordRest(String password,Integer role,String username,Model model) throws Exception {
        User u = userService.findUserIdWithUserName(username);
        if (u != null) {
            if((password.indexOf(" ")!=-1) || (username.indexOf(" ")!=-1)){
                throw new CustomException("格式有错误，请认真填写");
            }
            userService.updateById(username,password,role,u.getUserid());
            model.addAttribute("msg","重置成功");
        } else {
            model.addAttribute("msg","没有该用户");

        }
        return "/admin/userPasswordRest";
    }
    // 本账户密码重置
    @RequestMapping("/passwordRest")
    public String passwordRestUI() throws Exception {
        return "/admin/passwordRest";
    }
    @RequestMapping(value = "/doPasswordRest", method = {RequestMethod.POST})
    public String doPasswordRest(String password, String password2,Model model) throws Exception {
        User user=new User();
        if (password.equals(password2)) {
            if((password.indexOf(" ")!=-1) || (password2.indexOf(" ")!=-1)){
                throw new CustomException("密码格式有错误，请认真填写");
            }
            user.setPassword(MDUtils.getMD5Password(password,registUtil.getSalt(Depository.getUSERNAME())).toString());
            userService.updateWhitPassword(user.getPassword());
            model.addAttribute("msg","本账户密码已重置，请重新登陆");
        }
        return "login";
    }

}
